package org.ddxz.liquibase.config;

import com.alibaba.druid.pool.DruidDataSource;
import liquibase.integration.spring.SpringLiquibase;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2024/3/17
 */

@Configuration
@MapperScan(basePackages = "org.ddxz.liquibase.*.mapper.clickhouse.**", sqlSessionTemplateRef = "ckSessionTemplate")
public class ClickHouseDatabaseConfig {
    @Bean("ckDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.ck")
    DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        return datasource;
    }

    @Bean(name = "ckSessionFactory")
    public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("ckDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        // 设置sql配置文件路径
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:config/mapper/*/ck/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * 创建管理器
     *
     * @param dataSource
     * @return
     */

    @Bean(name = "ckTransactionManager")
    public DataSourceTransactionManager mysqlTranscationManager(@Qualifier("ckDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 创建模板
     *
     * @param sqlSessionFactory
     * @return
     * @throws Exception
     */

    @Bean(name = "ckSessionTemplate")
    public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("ckSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "ckLiquibase")
    public SpringLiquibase liquibase(@Qualifier("ckDataSource") DataSource dataSource) {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        if("single".equals(type)) {
            liquibase.setChangeLog("classpath:liquibase/clickhouse/ddxz-clickhouse-single.xml");
        } else if ("replicated".equals(type)) {
            liquibase.setChangeLog("classpath:liquibase/clickhouse/ddxz-clickhouse-replicated.xml");
            liquibase.setCluster(cluster);
        } else {
            liquibase.setChangeLog("classpath:liquibase/clickhouse/ddxz-clickhouse-distribute.xml");
            liquibase.setCluster(cluster);
        }
        liquibase.setDatabaseChangeLogTable("version_change_log");
        liquibase.setDatabaseChangeLogLockTable("version_change_lock");
        return liquibase;
    }

    @Value("${ddxz.cluster.name:everdc_ck_cluster}")
    private String cluster;

    @Value("${ddxz.cluster.type:single}")
    private String type;
}
